//
//  ResetPWViewController.m
//  Project_IOS
//
//  Created by Jeff on 2022/1/18.
//

#import "ResetPWViewController.h"
#import "LoginNumberView.h"
#import "CodeNumberView.h"
#import <Masonry/Masonry.h>
#import "CodeMan.h"

@interface ResetPWViewController ()
@property (nonatomic, strong) CodeMan *codeMan;
@property (nonatomic, strong) CodeNumberView *numberView;

@property (nonatomic, strong) LoginNumberView *phoneView;
@property (nonatomic, strong) LoginNumberView *pwView;

@end

@implementation ResetPWViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleStr = @"修改密码";
    
//    [self.view addSubview:self.centerView];
//    [self.centerView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.size.mas_equalTo(CGSizeMake(343), 500)));
//            make.top.mas_equalTo(137));
//            make.centerX.mas_equalTo(0);
//    }];
    self.phoneView = [[LoginNumberView alloc] initPhone:false];
    self.phoneView.textField.placeholder = @"请输入要修改的密码";
    self.pwView = [[LoginNumberView alloc] initPhone:false];
    self.pwView.textField.placeholder = @"请确认密码";
    
    self.numberView = [[CodeNumberView alloc] init];
    [self.numberView.moreBtn addTarget:self action:@selector(sendCode) forControlEvents:UIControlEventTouchUpInside];
    
    [self.phoneView.textField addTarget:self action:@selector(textfieldContentChange:) forControlEvents:UIControlEventEditingChanged];
    [self.pwView.textField addTarget:self action:@selector(textfieldContentChange:) forControlEvents:UIControlEventEditingChanged];
    [self.numberView.textField addTarget:self action:@selector(textfieldContentChange:) forControlEvents:UIControlEventEditingChanged];
    
    [self.contentView addSubview:self.numberView];
    [self.numberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(110);
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(311, 52));
    }];
    
    [self.contentView addSubview:self.phoneView];
    [self.phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.numberView.mas_bottom).offset(30);
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(311, 52));
    }];
    
    [self.contentView addSubview:self.pwView];
    [self.pwView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.phoneView.mas_bottom).offset(20);
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(311, 52));
    }];
    
    [self.contentView addSubview:self.buttonView];
    [self.buttonView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(311, 48));
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(self.pwView.mas_bottom).offset(45);
    }];
    self.buttonView.btnStr = @"确认";
    [self.buttonView.button addTarget:self action:@selector(loginBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
    self.buttonView.avable = NO;
    [self sendCode];
    
}

- (void)sendCode {
//    MJWeakSelf
//    [self.codeMan sendCode:self.mobile complete:^(BOOL success) {
//        if (success) {
//            [weakSelf.numberView startCount:60];
//        }
//    }];
}

- (void)loginBtnOnClick {
    
//    if (self.numberView.textField.text.length <= 0) {
//        [MBProgressHUD showMessage:@"请输入验证码！"];
//        return;
//    }
//    if (self.phoneView.textField.text.length <= 0) {
//        [MBProgressHUD showMessage:@"请输入密码！"];
//        return;
//    }
//    if (self.pwView.textField.text.length <= 0) {
//        [MBProgressHUD showMessage:@"请输入确认密码！"];
//        return;
//    }
//    if (![self.pwView.textField.text isEqualToString:self.phoneView.textField.text]) {
//        [MBProgressHUD showMessage:@"两次密码输入不一致！"];
//        return;
//    }
    
    [self requestForResetPwd];
   
}

- (void)requestForResetPwd {
    
//    [YNTNetworkManager requestPOSTwithURLStr:[NSString stringWithFormat:@"%@%@", ServerHost, URL_RESET_PWD] paramDic:@{@"mobile": self.mobile, @"newpassword": self.pwView.textField.text, @"captcha":self.numberView.textField.text} finish:^(id responseObject) {
//        NSDictionary *dic = responseObject;
//        if([dic[@"code"] integerValue] == 1) {
//            [LoginTools logPwdResetSuccess];
//            [self dismiss];
//        }else {
//            [MBProgressHUD showMessage:[NSString stringWithFormat:@"%@", dic[@"msg"]]];
//        }
//    } enError:^(NSError *error) {
//        [MBProgressHUD showMessage:error.localizedDescription];
//        NSLog(@"------------  %@", error);
//    }];
}

- (CodeMan *)codeMan {
    if (!_codeMan) {
        _codeMan = [CodeMan new];
    }
    return _codeMan;
}

- (void)textfieldContentChange:(UITextField *)field {
    if (self.phoneView.textField.text.length > 0 && self.numberView.textField.text != nil && self.pwView.textField.text.length > 0) {
        self.buttonView.avable = true;
    }else{
        self.buttonView.avable = false;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
